package com.interest.plus2.slice;

import com.interest.plus2.ResourceTable;
import com.interest.plus2.provider.MainPagerProvider;
import com.interest.plus2.view.CustomMainTabButtom;
import com.interest.plus2.view.TabChangeListener;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.PageSlider;

import java.util.ArrayList;

/**
 * qpp首页
 */
public class MainAbilitySlice extends AbilitySlice {
    PageSlider pageSlider;
    CustomMainTabButtom cusMainTabButtom;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_home);
        initView();
    }

    private void initView() {

        cusMainTabButtom = (CustomMainTabButtom) findComponentById(ResourceTable.Id_custoMainTabButtom);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        pageSlider.setPageCacheSize(4);
        //切换时间
        pageSlider.setPageSwitchTime(1000);
        //回弹效果
        pageSlider.setReboundEffect(true);
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int itemPos, float itemPosOffset, int itemPosPixles) {

            }

            @Override
            public void onPageSlideStateChanged(int state) {
            }

            @Override
            public void onPageChosen(int itemPos) {
                cusMainTabButtom.setTabSelect(itemPos);
            }
        });
        pageSlider.setProvider(new MainPagerProvider(getData(), this));

        cusMainTabButtom.setTabChangeListener(new TabChangeListener() {
            @Override
            public void onTabChangeListener(int index) {
                pageSlider.setCurrentPage(index);
            }
        });


    }

    private ArrayList<MainPagerProvider.DataItem> getData() {
        ArrayList<MainPagerProvider.DataItem> dataItems = new ArrayList<>();
        dataItems.add(new MainPagerProvider.DataItem("Page A"));
        dataItems.add(new MainPagerProvider.DataItem("Page B"));
        dataItems.add(new MainPagerProvider.DataItem("Page C"));
        dataItems.add(new MainPagerProvider.DataItem("Page D"));
        return dataItems;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
